/**
 * @param {number[][]} matrix
 * @return {boolean}
 */
 var checkValid = function(matrix) {
  const n = matrix.length
  const check = (matrix) => {
    for(let i = 0; i<n; i++){
      let arr = [...matrix[i]].sort((a,b) => a-b)
      for(let j = 0; j<n; j++){
        if(arr[j] !== j+1) return false
      }
    }
    return true
  }
  if(!check(matrix)) return false
  const m = new Array(n).fill(0).map(()=> new Array(n))
  for(let i = 0; i<n; i++){
    for(let j = 0; j<n; j++){
      m[j][i] = matrix[i][j]
    }
  }
  return check(m)
};